package com.learning.cloud.controller;

import com.learning.cloud.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * description:
 *
 * @author minghuiZhang
 * @date created in  15:28 2018/4/28
 * modified by
 */
@RestController
@Slf4j
public class MovieController {
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@GetMapping("/movie/{id}")
	public ResponseEntity<User> findById(@PathVariable("id") Long id){

		return this.restTemplate.getForEntity( "http://microservice-provider-user:7900/simple/" + id,User.class);
	}


	@GetMapping("/test")
	public String test(){
		ServiceInstance serviceInstance1 = loadBalancerClient.choose("microservice-provider-user");
		ServiceInstance serviceInstance2 = loadBalancerClient.choose("microservice-provider-user1");
		log.info("1111:  {}:{},{}",serviceInstance1.getHost(),serviceInstance1.getPort(),serviceInstance1.getServiceId());
		log.info("2222:  {}:{},{}",serviceInstance2.getHost(),serviceInstance2.getPort(),serviceInstance2.getServiceId());
		return "1111";
	}
}
